package com.ttbj.controller.notify;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ttbj.constant.order.PayEnum;
import com.ttbj.handler.NotifyHandler;
import com.ttbj.product.api.DriverFacade;
import com.ttbj.product.api.LocationFacade;
import com.ttbj.product.api.OrderFacade;
import com.ttbj.product.api.OriginalPayFacade;
import com.ttbj.product.api.OwnerFacade;
import com.ttbj.product.api.PayFacade;
import com.ttbj.product.api.WaybillFacade;
import com.ttbj.product.bean.dto.DriverDto;
import com.ttbj.product.bean.dto.EntitieDto;
import com.ttbj.product.bean.dto.OrderDto;
import com.ttbj.product.bean.dto.OwnerDto;
import com.ttbj.product.bean.dto.PassiveScanQRCodeDataDto;
import com.ttbj.util.Assert;

/**
 * 回调 1.订单和支付订单的关联 2.订单二维码的生成 3.司机派单 4.分账
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/notify")
public class NotifyController {
	private static Logger log = LoggerFactory.getLogger(NotifyController.class);

	@Autowired
	private PayFacade payFacade = null;

	@Autowired
	private OwnerFacade ownerFacade = null;
 
	@Autowired
	private OriginalPayFacade originalPayFacade = null; 
	

	@RequestMapping(value = "/applet.do", method = RequestMethod.POST)
	public void notity(HttpServletRequest request, HttpServletResponse response, PassiveScanQRCodeDataDto data) throws Exception {
		log.info("data获取到的数据:{}", data);
		response.getWriter().print("success");
		String orderNum = data.getMerOrderNum();
		String openId = data.getSub_openid();
		String amount = data.getTranAmt();
		/* 对订单状态、分账 */
		OrderDto orderDto = payFacade.appletNotify(orderNum, openId, amount, data);
		if (Assert.assertNonNullField0(orderDto, "orderId")) {
		}
		/* 寄件人资料获取 */
		OwnerDto sender = ownerFacade.getOwner(openId, orderDto.getOrderId(), 0);
		if (Assert.assertNonNullField0(sender, "ownerLat", "ownerLng")) {
			/* 异常单 */
		}
	}

	/**
	 * 原生小程序的回调处理
	 * 1.支付订单编号和订单id 2.订单二维码生成 3.派单
	 *原始回调
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value = "/original/applet.do", method = RequestMethod.POST)
	public void notityOriginal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, String> paramsResult = NotifyHandler.getParamsResult(request, response);
		if(paramsResult == null || !paramsResult.containsKey("out_trade_no")){
			return;
		}
		String orderNum = paramsResult.get("out_trade_no"); // 订单编号
		String openId = paramsResult.get("openid"); //
		String attach = paramsResult.get("attach");
		String transaction_id = paramsResult.get("transaction_id");
		originalPayFacade.payNotify( orderNum,openId,attach,transaction_id,PayEnum.WX.getWay());
	}

}
